package com.example.controller;


import com.example.annotation.RepeatSubmit;
import com.example.constant.RedisKey;
import com.example.controller.request.ConfirmOrderRequest;
import com.example.controller.request.ProductOrderPageRequest;
import com.example.enums.ClientTypeEnum;
import com.example.enums.ProductOrderPayTypeEnum;
import com.example.interceptor.LoginInterceptor;
import com.example.service.ProductOrderService;
import com.example.utils.CommonUtil;
import com.example.utils.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * 订单接口视图控制器
 *
 * @author yinxin
 * @since 2022-06-13
 */
@RestController
@RequestMapping("/api/order/v1")
@Slf4j
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/token")
    public JsonData getOrderToken() {
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        String token = CommonUtil.getRandomCode(32);

        //order:submit:账户:随机token
        String key = String.format(RedisKey.SUBMIT_ORDER_TOKEN_KEY, accountNo, token);

        //令牌有效时间30分钟
        redisTemplate.opsForValue().set(key, String.valueOf(Thread.currentThread().getId()), 30, TimeUnit.MINUTES);

        return JsonData.buildSuccess(token);


    }

    /**
     * 分页接口
     *
     * @return
     */
    @GetMapping("/page")
    public JsonData page(@RequestBody ProductOrderPageRequest request) {
        return productOrderService.page(request);
    }

    /**
     * 查询订单状态
     *
     * @param outTradeNo
     * @return
     */
    @GetMapping("query_state")
    public JsonData getState(@RequestParam("out_trade_no") String outTradeNo) {
        return productOrderService.getOrderState(outTradeNo);
    }

    /**
     * 下单接口
     *
     * @param orderRequest
     * @param response     自定义注解防重复提交 先调用token接口获取订单token后才能访问此端口防止表单重复提交
     */
    @PostMapping("/confirm")
    @RepeatSubmit(limitType = RepeatSubmit.Type.PARAM)
    public JsonData confirmOrder(@RequestBody ConfirmOrderRequest orderRequest, HttpServletResponse response) {
        JsonData result = productOrderService.confirmOrder(orderRequest);
        return result;
        /*if (result.getCode() == 200) {
            //端类型
            String client = orderRequest.getClientType();
            //支付类型
            String payType = orderRequest.getPayType();
            //如果是支付宝支付，跳转网页，sdk除外
            if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.ALI_PAY.name())) {
                //支付宝支付各种不同类型的终端渲染 TODO
                if (client.equalsIgnoreCase(ClientTypeEnum.PC.name())) {

                } else if (client.equalsIgnoreCase(ClientTypeEnum.APP.name())) {

                } else if (client.equalsIgnoreCase(ClientTypeEnum.H5.name())) {

                }
            } else if (payType.equalsIgnoreCase(ProductOrderPayTypeEnum.WECHAT_PAY.name())) {
                //微信支付 TODO
                CommonUtil.sendJsonMessage(response, result);
            }
        } else {
            log.error("创建订单失败{}", result);
            CommonUtil.sendJsonMessage(response, response);
        }*/
    }


}

